#pragma once
#include "Comm.h"

namespace COMM
{
	class COMM_API Buffer
	{
	public:
		explicit Buffer(size_t size=0);
		Buffer(const void* ptr, size_t size);
		Buffer(const char* src);
		Buffer(const Buffer& rhs);
		Buffer& operator=(const Buffer& rhs);
		virtual ~Buffer(void);
		void release();

		operator char*();
		operator const char*() const;
		char* get();
		const char* get() const;
		size_t size() const;
		Buffer& memset(int val=0);
		Buffer& resize(size_t size);
		Buffer& consume(size_t size);
		Buffer& append(const void* ptr, int size);
		Buffer& append(const Buffer& rhs);

		static char* strdup(const char* src);// strdup has bugs in windows linked in different CRT.
		static void* malloc(size_t size);
		static void* calloc(size_t num, size_t size);
		static void* realloc(void* ptr, size_t size);
		static void free(void* ptr);

	private:
		char* m_buffer;
		size_t m_size;
	};


	class String;
	class COMM_API StringList
	{
	public:
		StringList();
		StringList(const StringList& rhs);
		StringList& operator=(const StringList& rhs);
		virtual ~StringList();

		int count() const;
		String& get(int index);
		const String& get(int index) const;
		String& operator[](int index);
		const String& operator[](int index) const;
		StringList& append(const String& inst);
		StringList& insert(int index, const String& inst);	
		StringList& remove(int index);
		StringList& clear();
		StringList& operator<<(const String& inst);

		String join(const char* seperators) const;

	private:
		void* m_list;
	};


	class COMM_API String : public Buffer
	{
	public:
		static String Empty;
		explicit String(int length);
		String(const char* src=NULL);
		String(const String& rhs);

		char& operator[](int index);
		const char& operator[](int index) const;
		bool null() const;
		bool empty() const;
		int length() const;
		bool equal(const char* src, bool ignorecase=false) const;
		String& append(const char* src);
		String& operator+=(const char* src);
		String& operator<<(const char* src);
		String operator+(const char* src) const;
		bool operator==(const char* src) const;
		bool operator!=(const char* src) const;
		bool operator==(const String& src) const;
		bool operator!=(const String& src) const;
		int toint() const;
		long tolong() const;
		double todouble() const;
		String& tolower();
		String& toupper();
		String& reverse();
		String& trimleft();
		String& trimright();
		String& trim();
		String& replace(const char* substr, const char* newstr, bool replaceall=true);
		String& remove(const char* substr, bool removeall=true);
		bool find(const char* str) const;
		bool startwith(const char* str) const;
		bool endwith(const char* str) const;
		StringList split(const char* seperators) const;

		String& strcpy(const char* src);
		String& strcat(const char* src);
		String& vsprintf(const char* format, va_list args);
		String& sprintf(const char* format, ...);
		
		static bool equal(const char* src, const char* str, bool ignorecase=false);
		static String join(const char* src, const char* str);
		static String tolower(const char* src);
		static String toupper(const char* src);
		static String reverse(const char* src);
		static String trimleft(const char* src);
		static String trimright(const char* src);
		static String trim(const char* src);
		static bool find(const char* src, const char* str);
		static bool startwith(const char* src, const char* str);
		static bool endwith(const char* src, const char* str);
		static StringList split(const char* src, const char* seperators);
		static String format(const char* format, ...);
		static String format(int size, const char* format, ...);
		
	};

}

